package com.xinlu.common.game.bo;

import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.xinlu.common.exception.LogicException;
import com.xinlu.common.game.dao.ScSjglDao;
import com.xinlu.common.game.dao.TzjlDao;
import com.xinlu.common.game.dao.WcSjglDao;
import com.xinlu.common.game.value.JrtzVo;
import com.xinlu.common.game.value.TzjlVo;
import com.xinlu.common.game.value.ZhjlVo;
import com.xinlu.common.game.value.ZjmxVo;
import com.xinlu.derek.common.BaseBo;
import com.xinlu.persistence.BankBack;
import com.xinlu.persistence.ScSjgl;
import com.xinlu.persistence.Tzjl;
import com.xinlu.persistence.Users;
import com.xinlu.user.dao.UsersDao;
import com.xinlu.util.tool.DateUtil;
import com.xinlu.util.tool.PageObject;

public class TzjlBo extends BaseBo {

	// -------- TzjlDao注入 --------------
	private TzjlDao tzjlDao;
	private WcSjglDao wcSjglDao;
	private ScSjglDao scSjglDao;

	public void setWcSjglDao(WcSjglDao wcSjglDao) {
		this.wcSjglDao = wcSjglDao;
	}

	public void setScSjglDao(ScSjglDao scSjglDao) {
		this.scSjglDao = scSjglDao;
	}

	public void setTzjlDao(TzjlDao tzjlDao) {
		this.tzjlDao = tzjlDao;
	}

	private UsersDao usersDao;

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	// ------- 注入结束 ---------------

	public void saveTzjlBo(double addMoney, double onemoney, String protype, String codes, String expect, int beishu, String username, String xl, String czlx, int zhqs, int sfzh, int zjtzzh) {

		Tzjl po = new Tzjl();

		// 倍数
		po.setBs(String.valueOf(beishu));
		// 投注号码
		po.setTzhm(codes);
		// 期号
		po.setQh(expect);
		// 是否返回: 0未返还
		po.setSffh(0);
		// 是否追号:0不追号
		po.setSfzh(sfzh);
		// 中奖后停止追号
		po.setZjtzzh(zjtzzh);
		// 当期金额
		po.setDqje(addMoney + "");
		// 单证状态
		po.setDzzt("正常");
		// 游戏类型
		po.setYxlx(protype);
		// 用户名
		po.setYhm(username);
		// 系列:1700、1800、1900
		po.setXl(xl);
		// 彩种类型:WC、SC
		po.setCzfl(czlx);
		// 中奖状态
		po.setZjzt("未中奖");
		// 追号期数:默认为1
		po.setZhqs(1);
		// 代购时间
		po.setDgsj(new Timestamp(new Date().getTime()));
		// 单次提交金额
		po.setDctjzje(addMoney * beishu + "");
		// ---- 通过验证，需要去查询时间表
		po.setTgyz(1);

		// po = this.tzjlDao.save(po);

		po.setZhm(po.getId() + "");
		po.setDzh(po.getXl() + "_" + po.getQh() + po.getYxlx() + po.getId() + "y");

		this.tzjlDao.update(po);
	}

	/**
	 * 加载投注记录
	 * 
	 * @param caiZhong
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public PageObject loadTzjls(String yhm, String caiZhong, String bgnDate, String endDate, int pageNo) throws IllegalAccessException, InvocationTargetException {
		PageObject po = tzjlDao.loadTzjls(yhm, caiZhong, bgnDate, endDate, pageNo);
		List<TzjlVo> lst = po.getItems();
		for(TzjlVo tzjlVo : lst){
			if(tzjlVo!=null){
				tzjlVo.setWfjczwm(ConstantLstBo.getwfjcNotContainCaiZhong(tzjlVo.getWfjczwm()));
			}
		}
		return po;
	}

	/**
	 * 加载追号记录
	 * 
	 * @param caiZhong
	 * @return
	 */
	public PageObject loadZhjls(String caiZhong, String zhzt, String bgnDate, String endDate,int pageNo) {
		PageObject po = tzjlDao.loadZhjls(caiZhong, zhzt, bgnDate, endDate,pageNo);
		List lst = po.getItems();
		List<ZhjlVo> zhjls = new ArrayList<ZhjlVo>();
		if (lst != null) {
			for (int i = 0; i < lst.size(); i++) {
				ZhjlVo vo = new ZhjlVo();
				TzjlVo tzjl = (TzjlVo)lst.get(i);
				vo.setWfjczwm(ConstantLstBo.getwfjcNotContainCaiZhong(tzjl.getWfjczwm()));
				vo.setAllmoney(tzjl.getDqje());
				vo.setDgsj(DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, tzjl.getDgsj()));
				vo.setDzh(tzjl.getDzh());
				vo.setQh(tzjl.getQh());
				vo.setWcqs(tzjlDao.getWcqs(tzjl.getId()));
				vo.setZhqs(tzjl.getZhqs());
				vo.setZhzt(tzjl.getSffh() == 1 ? "已完成" : "进行中");
				zhjls.add(vo);
			}
		}

		po.setItems(zhjls);
		return po;
	}

	/**
	 * 加载方案管理
	 * 
	 * @param caiZhong
	 * @param bgnDate
	 * @param endDate
	 * @param dzh
	 * @param qh
	 * @param yhm
	 * @param zhm
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public PageObject loadFagls(Users crtUser, String caiZhong, String bgnDate, String endDate, String dzh, String qh, String xjyhm, String zhm, int pageNo) throws IllegalAccessException,
			InvocationTargetException {

		List yhms = new ArrayList<String>();
		// 如果下级用户名为空，则加载出当前用户下的所有子用户
		if (ConstantLstBo.isNullOrEmpty(xjyhm)) {
			List<Users> yhs = usersDao.loadXjyhs(crtUser);
			for (Users user : yhs) {
				yhms.add(user.getUsername());
			}
		} else {
			yhms.add(xjyhm);
		}

		PageObject po = tzjlDao.loadFagls(caiZhong, bgnDate, endDate, dzh, qh, yhms, zhm, pageNo);
		return po;
	}

	/**
	 * 根据id加载投注明细
	 * 
	 * @param parseInt
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public TzjlVo loadTzmx(String dzh) throws IllegalAccessException, InvocationTargetException {

		List lst = tzjlDao.loadTzmx(dzh);
		if (lst != null && lst.size() > 0) {
			TzjlVo vo = new TzjlVo();
			Object[] values = (Object[]) lst.get(0);
			Tzjl tzjl = (Tzjl) values[0];
			BeanUtils.copyProperties(vo, tzjl);
			vo.setWfjczwm(values[1].toString());

			if ("未开奖".equals(tzjl.getZjzt()) && "正常".equals(tzjl.getDzzt())) {
				// 判断是否能撤单
				Date now = DateUtils.addMinutes(new Date(), 2);
				Date dgjssj = getDgjssj(tzjl.getYxlx(), tzjl.getQh(), tzjl.getDgsj());
				if (now.compareTo(dgjssj) >= 0) {
					vo.setNfcd(0);
				} else {
					vo.setNfcd(1);
				}
			} else {
				vo.setNfcd(0);
			}

			return vo;
		} else {
			throw new LogicException("找不到单证号为" + dzh + "的投注记录.");
		}
	}

	/**
	 * 撤单
	 * 
	 * @param parseInt
	 */
	public void cheDan(String dzh) {

		List lst = tzjlDao.loadTzmx(dzh);
		if (lst != null && lst.size() > 0) {
			Object[] values = (Object[]) lst.get(0);
			Tzjl tzjl = (Tzjl) values[0];
			if("已撤单".equals(tzjl.getDzzt())){
				throw new LogicException("该记录已经撤单过.");
			}

			tzjl.setSffh(1);
			tzjl.setZjzt("已撤单");
			tzjl.setDzzt("已撤单");
			tzjlDao.update(tzjl);
		} else {
			throw new LogicException("找不到单证号为" + dzh + "的投注记录.");
		}
	}

	/**
	 * 
	 * Function : 获取资料详细
	 * 
	 * @param type
	 * @param yxlx
	 * @param username
	 * @param startdate
	 * @param enddate
	 * @return Derek 2011-5-29 下午09:11:48
	 */
	public List getZjmxList(String type, String yxlx, String username, String startdate, String enddate) {

		Date start = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MI_SS, startdate);
		Date end = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MI_SS, enddate);

		if ("all".equals(yxlx)) {

			yxlx = "CQSSC%,FSD%,HLJSSC%,JXSSC%,PLS%";

		} else {

			yxlx += "%";
		}

		if ("all".equals(type)) {

			type = "取款,存款,撤单,派奖,返点,返奖";
		} else if ("QK".equals(type)) {

			type = "取款";
		} else if ("CK".equals(type)) {

			type = "存款";
		} else if ("CD".equals(type)) {

			type = "撤单";
		} else if ("PJ".equals(type)) {

			type = "派奖";
		} else if ("FD".equals(type)) {

			type = "返点";
		} else if ("FJ".equals(type)) {

			type = "返奖";
		}

		List<ZjmxVo> voList = null;
		ZjmxVo vo = null;

		if (StringUtils.isNotEmpty(yxlx)) {

			String[] yx = yxlx.split(",");
			List list = new ArrayList();

			for (String str : yx) {

				List reList = this.tzjlDao.findAllZjmxList(str, start, end, username);

				if (reList != null && reList.size() > 0) {

					list.addAll(reList);
				}
			}

			voList = new ArrayList<ZjmxVo>();

			if (list != null && list.size() > 0) {
				Object[] objs = null;
				Tzjl tzjl = null;
				BankBack bankBack = null;

				for (int i = 0; i < list.size(); i++) {

					objs = (Object[]) list.get(i);
					bankBack = (BankBack) objs[1];
					vo = new ZjmxVo();

					vo.setChangeMoney(bankBack.getBAfter() - bankBack.getBBefor());
					vo.setAllmoney(bankBack.getBAfter());
					vo.setDgsj(DateUtil.getFormatDate(DateUtil.YYYY_MM_DD_HH_MI_SS, bankBack.getAddtime()));
					vo.setWfjczhw((String) objs[0]);
					vo.setFollows(bankBack.getFollows());
					vo.setState(bankBack.getProstate());
					vo.setQh(bankBack.getExpect());
					vo.setDzh(bankBack.getLotteryid());

					voList.add(vo);
				}
			}
		}

		if (StringUtils.isNotEmpty(type)) {

			List cqkList = new ArrayList();
			String[] pes = type.split(",");

			for (int i = 0; i < pes.length; i++) {

				List partList = this.tzjlDao.findAllCkqkList(pes[i], start, end, username);

				if (partList != null && partList.size() > 0) {

					cqkList.addAll(partList);
				}
			}

			if (cqkList != null && cqkList.size() > 0) {

				BankBack bankBack = null;

				for (int i = 0; i < cqkList.size(); i++) {

					bankBack = (BankBack) cqkList.get(i);
					vo = new ZjmxVo();

					vo.setAllmoney(bankBack.getBAfter());
					vo.setChangeMoney(bankBack.getBAfter() - bankBack.getBBefor());
					vo.setWfjczhw(bankBack.getType());
					vo.setFollows(bankBack.getFollows());
					vo.setDzh(bankBack.getLotteryid());
					vo.setDgsj(DateUtil.getFormatDate(DateUtil.YYYY_MM_DD_HH_MI_SS, bankBack.getAddtime()));
					vo.setQh(bankBack.getExpect());
					vo.setState(bankBack.getProstate());

					voList.add(vo);
				}
			}
		}

		return voList;
	}

	/**
	 * 
	 * Function : 获取资料详细
	 * 
	 * @param type
	 * @param yxlx
	 * @param username
	 * @param startdate
	 * @param enddate
	 * @return Derek 2011-5-29 下午09:11:48
	 */
	public List getTzmxList(String yxlx, String username) {

		if ("all".equals(yxlx)) {

			yxlx = "CQSSC%,FSD%,HLJSSC%,JXSSC%,PLS%";

		} else {

			yxlx += "%";
		}

		List<ZjmxVo> voList = null;
		ZjmxVo vo = null;

		if (StringUtils.isNotEmpty(yxlx)) {

			String[] yx = yxlx.split(",");
			List list = new ArrayList();

			for (String str : yx) {

				List reList = this.tzjlDao.findAllTzmxList(str, username);

				if (reList != null && reList.size() > 0) {

					list.addAll(reList);
				}
			}

			voList = new ArrayList<ZjmxVo>();

			if (list != null && list.size() > 0) {
				Object[] objs = null;
				Tzjl tzjl = null;
				BankBack bankBack = null;

				for (int i = 0; i < list.size(); i++) {

					objs = (Object[]) list.get(i);
					bankBack = (BankBack) objs[1];
					vo = new ZjmxVo();

					vo.setChangeMoney(bankBack.getBAfter() - bankBack.getBBefor());
					vo.setAllmoney(bankBack.getBAfter());
					vo.setDgsj(DateUtil.getFormatDate(DateUtil.YYYY_MM_DD_HH_MI_SS, bankBack.getAddtime()));
					vo.setWfjczhw((String) objs[0]);
					vo.setFollows(bankBack.getFollows());
					vo.setState(bankBack.getProstate());
					vo.setQh(bankBack.getExpect());
					vo.setDzh(bankBack.getLotteryid());

					voList.add(vo);
				}
			}
		}

		return voList;
	}

	/**
	 * 获取代购结束时间
	 * 
	 * @param caiZhong
	 * @param qh
	 * @param dgsj
	 * @return
	 */
	public Date getDgjssj(String caiZhong, String qh, Date dgsj) {
		Date dgjssj = null;
		caiZhong = caiZhong.toLowerCase();
		String today = DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, new Date()) + " ";
		if (caiZhong.startsWith("cqssc")) {
			String hhmmss = wcSjglDao.getDgjssjByQh(qh.substring(qh.length() - 3), "cqssc").split(" ")[1];
			dgjssj = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MI_SS, today + hhmmss);

		} else if (caiZhong.startsWith("jxssc")) {
			String hhmmss = wcSjglDao.getDgjssjByQh(qh.substring(qh.length() - 2), "jxssc").split(" ")[1];
			dgjssj = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MI_SS, today + hhmmss);

		} else if (caiZhong.startsWith("hljssc")) {
			Date dgsjNyr = DateUtil.getDate(DateUtil.YYYY_MM_DD, DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, dgsj));
			int dayNum = (int) DateUtil.dateDiff(3, DateUtil.getDate(DateUtil.YYYY_MM_DD, "2010/02/19"), dgsjNyr);
			int qhs = Integer.parseInt(qh.substring(qh.length() - 5));

			int dayNum2 = 18464 + (dayNum - 1) * 84;
			int jdz = Math.abs(qhs - dayNum2);

			String zzqh = "";
			if (jdz > 84) {
				BigInteger bi1 = BigInteger.valueOf(jdz);
				BigInteger bi2 = BigInteger.valueOf(84);
				zzqh = bi1.mod(bi2).toString();
			} else if (jdz < 10) {
				zzqh = "0" + jdz;
			} else if (jdz == 0) {
				zzqh = "01";
			}

			String hhmmss = wcSjglDao.getDgjssjByQh(zzqh, "hljssc").split(" ")[1];
			dgjssj = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MI_SS, today + hhmmss);

		} else if (caiZhong.startsWith("fsd") || caiZhong.startsWith("pls")) {
			Date jndyt = DateUtil.getDate(DateUtil.YYYY_MM_DD, DateUtil.getFormatDate(DateUtil.YYYY, new Date()) + "/01/01");
			int qhts = Integer.parseInt(qh.substring(qh.length() - 3)) - 1;
			Date dqllsj = DateUtils.addDays(jndyt, qhts);

			ScSjgl scSjgl = scSjglDao.loadTopOne(caiZhong.startsWith("fsd") ? "fsd" : "pls");
			Date cjkssj = DateUtil.getDate(DateUtil.YYYY_MM_DD, DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, scSjgl.getCjkssj()));

			if (dqllsj.compareTo(cjkssj) < 0) {
				String hhmmss = DateUtil.getFormatDate(DateUtil.HH_MI_SS, scSjgl.getDgjssj());
				dgjssj = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MI_SS, DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, dqllsj) + " " + hhmmss);
			} else {
				String hhmmss = DateUtil.getFormatDate(DateUtil.HH_MI_SS, scSjgl.getDgjssj());
				dgjssj = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MI_SS, DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, DateUtils.addDays(dqllsj, scSjgl.getXxts())) + " " + hhmmss);
			}
		}

		return dgjssj;
	}

	// public void getZhjlDetailList(String yxlx, String zhzt){
	//		
	//		
	//		
	// }
	
	
	public List<JrtzVo> getJrtzList(String czlx, String username){
		
		//	获取当日的时间
		Timestamp today = DateUtil.getTimestamp(DateUtil.getDate(DateUtil.YYYY_MM_DD, DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, DateUtil.getCurrentdate())));
		
		List reList = this.tzjlDao.getJrtzList(czlx, username, today);
		List<JrtzVo> voList = null;
		
		if (reList != null && reList.size() > 0) {
			voList = new ArrayList<JrtzVo>();
			Object[] objs = null;
			Tzjl tzjl = null;
			JrtzVo vo = null;
			String zwm = null;
			
			for (int i = 0; i < reList.size(); i++) {
				
				objs = (Object[]) reList.get(i);
				tzjl = (Tzjl) objs[0];
				zwm = (String)objs[1];
				
				String[] tzhms = tzjl.getTzhm().split("$");
				if (tzhms != null && tzhms.length > 0) {
					
					for (String tzhm : tzhms) {
						
						vo = new JrtzVo();
						
						vo.setQh(tzjl.getQh());
						vo.setTzhm(tzhm);
						vo.setDgsj(DateUtil.getFormatDate(DateUtil.YYYY_MM_DD_HH_MI_SS, tzjl.getDgsj()));
						
						if (zwm.contains("重庆") || zwm.contains("江西")) {
							
							vo.setWflxjczwm(zwm.substring(5, zwm.length()));
						}else if (zwm.contains("黑龙江")) {
							
							vo.setWflxjczwm(zwm.substring(6, zwm.length()));
						}else if (zwm.contains("福彩")) {
							
							vo.setWflxjczwm(zwm.substring(4, zwm.length()));
						}else if (zwm.contains("排列三")) {
							
							vo.setWflxjczwm(zwm.substring(3, zwm.length()));
						}
						
						voList.add(vo);
					}
					
				}
				
			}
		}
		
		return voList;
	}

}
